package com.photopicker.main.activity;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.photopicker.ImageDownLoadCallBack;
import com.photopicker.R;
import com.photopicker.base.BaseActivity;
import com.photopicker.util.DownLoadImageService;

import java.io.File;

public class SaveImageActivity extends BaseActivity implements View.OnClickListener{
    public static  final int MSG_VISIBLE=1;
    public static  final int MSG_ERROR=2;
     int delayTime=200;
    private ImageView iv_image;
    private Button btn_save_image;
    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initViews() {
        iv_image = (ImageView) findViewById(R.id.iv_image);
        btn_save_image = (Button) findViewById(R.id.btn_save_image);
    }

    @Override
    protected void initEvents() {
        btn_save_image.setOnClickListener(this);
        path = "http://www.eoeandroid.com/uc_server/data/avatar/000/64/69/60_avatar_middle.jpg";
        Glide.with(this).load(path).error(R.mipmap.ic_launcher).into(iv_image);
    }

    @Override
    protected int getContentViewLayoutId() {
        return R.layout.activity_save_image;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_save_image:
                onDownLoad(path);
                break;
        }
    }

    private void onDownLoad(String url) {
        DownLoadImageService service = new DownLoadImageService(getApplicationContext(), url,
                new ImageDownLoadCallBack() {

                    @Override
                    public void onDownLoadSuccess(File file) {
                    }
                    @Override
                    public void onDownLoadSuccess(Bitmap bitmap) {
                        // 在这里执行图片保存方法
                        Message message = new Message();
                        message.what = MSG_VISIBLE;
                        handler.sendMessageDelayed(message, delayTime);
                    }

                    @Override
                    public void onDownLoadFailed() {
                        // 图片保存失败
                        Message message = new Message();
                        message.what = MSG_ERROR;
                        handler.sendMessageDelayed(message, delayTime);
                    }
                });
        //启动图片下载线程
        new Thread(service).start();
    }
     public Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case MSG_ERROR:
                    showToast("保存图片失败");
                    break;
                case MSG_VISIBLE:
                    showToast("保存图片成功");
                    break;
            }
        }
    };

    @Override
    protected void showToast(String msg) {
        super.showToast(msg);
    }
}
